#include "mini.kit/datetime.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

namespace MINIKIT
{

time_t      DateTime::now()
{
    return ::time(NULL);
}

time_t      DateTime::parse(const std::string &dt_string, const std::string &dt_format)
{
    time_t    t;
    struct tm tms;

    ::strptime( dt_string.c_str(), dt_format.c_str(), &tms);
    t = ::mktime(&tms);
    if( -1 == t ){
        //fprintf(stderr, "err\n");
        t = 0;
    }
    return t;
}

std::string DateTime::format(const time_t dt_stamp, const std::string &dt_format)
{
    std::string dt_string;
    size_t      dt_size = 0;
    struct tm tms;

    ::localtime_r(&dt_stamp, &tms);
    dt_string.resize(128);
    dt_size = ::strftime((char*)dt_string.c_str(), dt_string.size(), dt_format.c_str(), &tms);
    dt_string.resize(dt_size);
    return dt_string;
}



};//namespace MINIKIT


